# -*- coding:utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2Json_v2 import Table2Json
import random

from com.zzh.utils.pagingUtils import MyPaging
from com.zzh.utils.sqlUtils.SqlUtils import SqlUtils
from com.zzh.utils.requestFieldMapDBField import fieldMap_Chen


class HotDao(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections_v2()

    def insertHot(self, info):
        """
        添加 hot 信息
        :param info:
        :return:
        """
        hotName = info['hotName']
        hotId = info['hotId']
        hotType = info['hotType']

        sql = "insert into zzh_hot (hot_name, hot_id, hot_type) VALUES " \
              "(%s, %s, %s)"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [hotName, hotId, hotType])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def findHotById(self, hotId):
        """
        根据ID查询 hot 信息
        :param hotId:
        :return:
        """
        sql = "select * from zzh_hot WHERE id=%s"

        cur = self.conn.cursor()
        rs = cur.execute(sql, [hotId])
        jsons = None
        if rs:
            list0 = cur.fetchall()
            jsons = Table2Json.hot_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons

    def findHotByConditon(self, **kwargs):
        """
        hot表的条件查询
        :param kwargs:
        :return:
        """
        pageIndex = kwargs['pageIndex']
        pageSize = kwargs['pageSize']
        conditions = kwargs['conditions']
        multiSort = kwargs['multiSort']

        fieldMap = fieldMap_Chen.hotFieldMap
        whereSentence = SqlUtils.getOption(conditions, fieldMap)

        cur = self.conn.cursor()
        if multiSort:
            sortRule = SqlUtils.getSortRule(multiSort, fieldMap)
            sql = "SELECT * FROM zzh_hot WHERE " + whereSentence + " ORDER BY " + sortRule
        else:
            sql = "SELECT * FROM zzh_hot WHERE " + whereSentence
        rs = cur.execute(sql)

        jsons = None
        if rs:
            rsSet = cur.fetchall()
            list0 = MyPaging().getPage(rs, pageIndex, pageSize, rsSet)
            jsons = Table2Json.hot_list_to_json(list0)
        cur.close()
        self.conn.close()
        return jsons, rs

    def deleteHot(self, idArray):
        """
        根据ID删除信息，可以传递一个或者多个ID，ID以数组形式传递
        :param idArray:
        :return:
        """
        sql = "delete from zzh_hot WHERE id=%s"
        cur = self.conn.cursor()
        for idd in idArray:
            rs = cur.execute(sql, [idd])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def updateHot(self, info):
        """
        更新 history 表
        :param info:
        :return:
        """
        mId = info['id']
        hotName = info['hotName']
        hotId = info['hotId']
        hotType = info['hotType']

        sql = "update zzh_hot set hot_name=%s,hot_id=%s,hot_type=%s WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [hotName,hotId,hotType, mId])
        self.conn.commit()
        cur.close()
        self.conn.close()
        return rs

    def selectHot(self, hotId):
        """
        根据ID查询，返回元组
        :param hotId:
        :return:
        """
        sql = "select * from zzh_hot WHERE id=%s"
        cur = self.conn.cursor()
        rs = cur.execute(sql, [hotId])
        arr = cur.fetchone()
        cur.close()
        self.conn.close()
        return arr

    def getRandomItem(self):
        cur = self.conn.cursor()

        sql = "select * from zzh_item WHERE check_status_one=1 AND check_status_two=1 AND item_status=2 "
        rs = cur.execute(sql)
        if rs:
            ranInt = random.randint(0, rs - 1)
            rsSet = cur.fetchall()
            itemOne = rsSet[ranInt]

            json2 = Table2Json.item_list_to_json((itemOne,))
            return json2
        else:
            return None


